package novel.mzx.com.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;

import cn.jzvd.JZVideoPlayerStandard;
import novel.mzx.com.R;
import novel.mzx.com.activity.PhotoVideoActivity;
import novel.mzx.com.activity.PhotoViewActivity;
import novel.mzx.com.bean.CircleBean;
import novel.mzx.com.http.Api;
import novel.mzx.com.utils.MediaUtils;
import novel.mzx.com.utils.TimeUtil;
import novel.mzx.com.views.RoundImageView;
import novel.mzx.com.views.XCRoundImageView;




public class CircleAdapter extends CommonAdapter<CircleBean.Data> {
    Context mContext;

    public CircleAdapter(Context context, int layoutId, List<CircleBean.Data> datas) {
        super(context, layoutId, datas);

        mContext = context;

    }
    JZVideoPlayerStandard videoPlayer;
    @Override
    protected void convert(ViewHolder holder, final CircleBean.Data s, int position) {


        holder.setText(R.id.nick_tv,s.getJzCircleUsername());


        List<CircleBean.MyComment> myComment = s.getMyComment();
        holder.setText(R.id.comment_tv,myComment.size()+"");
        holder.setText(R.id.expand_tv,s.getJzCircleContent());
        RoundImageView one_image_iv0 = holder.getConvertView().findViewById(R.id.one_image_iv0);
        RoundImageView one_image_iv = holder.getConvertView().findViewById(R.id.one_image_iv);

        videoPlayer = holder.getConvertView().findViewById(R.id.m_video);
        String jzCircleFile = s.getJzCircleFile();
        if(null != jzCircleFile && jzCircleFile.length() > 0){
            if(!jzCircleFile.endsWith("mp4")){
                holder.setVisible(R.id.m_video,false);
                holder.setVisible(R.id.iv_play,false);
                holder.setVisible(R.id.one_image_iv0,true);
                holder.setVisible(R.id.rl_02,false);
                Glide.with(mContext).load(Api.mainImgHost+s.getJzCircleFile()).into(one_image_iv0);
            }else {
                holder.setVisible(R.id.iv_play,true);
                holder.setVisible(R.id.m_video,false);
                holder.setVisible(R.id.one_image_iv0,false);
                holder.setVisible(R.id.rl_02,true);
                Bitmap videoThumbnail = createVideoThumbnail(Api.mainImgHost + s.getJzCircleFile(), MediaStore.Images.Thumbnails.MINI_KIND);
                one_image_iv.setImageBitmap(videoThumbnail);
                //videoPlayer.setUp(Api.mainImgHost2 + s.getJzCircleFile(), JZVideoPlayerStandard.SCREEN_WINDOW_NORMAL, "");

            }

        }else {

        }

        XCRoundImageView head_iv = holder.getConvertView().findViewById(R.id.head_iv);
        Glide.with(mContext).load(Api.mainImgHost+s.getJzCircleLogo()).asBitmap().into(head_iv);

        holder.setOnClickListener(R.id.ll_show, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    public static Bitmap createVideoThumbnail(String filePath, int kind)
    {
        Bitmap bitmap = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try
        {
            if (filePath.startsWith("http://")
                    || filePath.startsWith("https://")
                    || filePath.startsWith("widevine://"))
            {
                retriever.setDataSource(filePath, new Hashtable<String, String>());
            }
            else
            {
                retriever.setDataSource(filePath);
            }
            bitmap = retriever.getFrameAtTime(0, MediaMetadataRetriever.OPTION_CLOSEST_SYNC); //retriever.getFrameAtTime(-1);
        }
        catch (IllegalArgumentException ex)
        {
            // Assume this is a corrupt video file
            ex.printStackTrace();
        }
        catch (RuntimeException ex)
        {
            // Assume this is a corrupt video file.
            ex.printStackTrace();
        }
        finally
        {
            try
            {
                retriever.release();
            }
            catch (RuntimeException ex)
            {
                // Ignore failures while cleaning up.
                ex.printStackTrace();
            }
        }

        if (bitmap == null)
        {
            return null;
        }

        if (kind == MediaStore.Images.Thumbnails.MINI_KIND)
        {//压缩图片 开始处
            // Scale down the bitmap if it's too large.
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            int max = Math.max(width, height);
            if (max > 512)
            {
                float scale = 512f / max;
                int w = Math.round(scale * width);
                int h = Math.round(scale * height);
                bitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);
            }//压缩图片 结束处
        }
        else if (kind == MediaStore.Images.Thumbnails.MICRO_KIND)
        {
            bitmap = ThumbnailUtils.extractThumbnail(bitmap,
                    96,
                    96,
                    ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
        }
        return bitmap;
    }

}
